From dc84416e90b4073b9c488e8644629848aaf37adb Mon Sep 17 00:00:00 2001 From: justbur Date: Sun, 12 Jul 2015 13:00:53 -0400 Subject: [PATCH] Add support for custom display functions --- README.org | 37 +++++++++++++++++++++++++++++++++++-- which-key.el | 29 ++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index 47c63bf82e8..d287cf0a63b 100644 --- a/README.org +++ b/README.org @@ -12,6 +12,7 @@ Rewrite of guide-key-mode for emacs. - [[#minibuffer][minibuffer]] - [[#side-window][side window]] - [[#frame][frame]] + - [[#custom][custom]] - [[#custom-string-replacement][Custom String Replacement]] - [[#key-based-replacement]["Key-Based" replacement]] - [[#key-and-description-replacement][Key and Description replacement]] @@ -88,8 +89,9 @@ Popup side window on bottom. For defaults use There are more options than the ones described here. All of the configurable variables are available through =M-x customize-group which-key=. ** Several Popup Types -There are three different popup types that which-key can use to display the -available keys. The variable =which-key-popup-type= decides which one is used. +There are three different popup types that which-key can use by default to +display the available keys. The variable =which-key-popup-type= decides which +one is used. *** minibuffer #+BEGIN_SRC emacs-lisp (setq which-key-popup-type 'minibuffer) @@ -115,6 +117,7 @@ Show keys in a side window. This popup type has further options: (setq which-key-side-window-max-height 0.25) #+END_SRC *** frame + #+BEGIN_SRC emacs-lisp (setq which-key-popup-type 'frame) #+END_SRC @@ -128,6 +131,36 @@ further options: ;; max height of which-key frame: number of lines (an integer) (setq which-key-frame-max-height 20) #+END_SRC + +*** custom +Write your own display functions! This requires you to write three functions, +=which-key/custom-popup-max-dimensions-function=, +=which-key/custom-show-popup-function=, and +=which-key/custom-hide-popup-function=. Refer to the documentation for those +variables for more information, but here is a working example (this is the +current implementation of side-window bottom). + + +#+BEGIN_SRC emacs-lisp +(setq which-key-popup-type 'custom) +(defun which-key/custom-popup-max-dimensions-function (ignore) + (cons + (which-key/height-or-percentage-to-height which-key-side-window-max-height) + (frame-width))) +(defun fit-horizonatally () + (let ((fit-window-to-buffer-horizontally t)) + (fit-window-to-buffer))) +(defun which-key/custom-show-popup-function (act-popup-dim) + (let* ((alist '((window-width . fit-horizontally) + (window-height . fit-window-to-buffer)))) + (if (get-buffer-window which-key--buffer) + (display-buffer-reuse-window which-key--buffer alist) + (display-buffer-in-major-side-window which-key--buffer 'bottom 0 alist)))) +(defun which-key/custom-hide-popup-function () + (when (buffer-live-p which-key--buffer) + (quit-windows-on which-key--buffer))) +#+END_SRC + ** Custom String Replacement You can customize the way the keys show in the buffer using three different replacement methods, each of which corresponds replacement alist. The basic idea diff --git a/which-key.el b/which-key.el index 506cadaeca1..c02dc32f909 100644 --- a/which-key.el +++ b/which-key.el @@ -97,11 +97,12 @@ the feature off." (const :tag "In first line" top) (const :tag "Hide" nil))) (defcustom which-key-popup-type 'minibuffer - "Supported types are minibuffer, side-window and frame." + "Supported types are minibuffer, side-window, frame, and custom." :group 'which-key :type '(radio (const :tag "Show in minibuffer" minibuffer) (const :tag "Show in side window" side-window) - (const :tag "Show in popup frame" frame))) + (const :tag "Show in popup frame" frame) + (const :tag "Use your custom display functions" custom))) (defcustom which-key-side-window-location 'right "Location of which-key popup when `which-key-popup-type' is side-window. Should be one of top, bottom, left or right." @@ -160,6 +161,21 @@ a percentage out of the frame's height." "Face for special keys (SPC, TAB, RET)" :group 'which-key) +;; Custom popup +(defvar which-key/custom-popup-max-dimensions-function nil + "Variable to hold a custom max-dimensions function. +Will be passed the width of the active window and is expected to +return the maximum height in lines and width in characters of the +which-key popup in the form a cons cell (height . width).") +(defvar which-key/custom-hide-popup-function nil + "Variable to hold a custom hide-popup function. +It takes no arguments and the return value is ignored.") +(defvar which-key/custom-show-popup-function nil + "Variable to hold a custom show-popup function. +Will be passed the required dimensions in the form (height . +width) in lines and characters respectively. The return value is +ignored.") + ;; Internal Vars ;; (defvar popwin:popup-buffer nil) (defvar which-key--buffer nil @@ -351,7 +367,8 @@ total height." (cl-case which-key-popup-type (minibuffer (which-key/hide-buffer-minibuffer)) (side-window (which-key/hide-buffer-side-window)) - (frame (which-key/hide-buffer-frame)))) + (frame (which-key/hide-buffer-frame)) + (custom (funcall #'which-key/custom-hide-popup-function)))) (defun which-key/hide-buffer-minibuffer () "Does nothing. Stub for consistency with other hide-buffer @@ -379,7 +396,8 @@ need to start the closing timer." (cl-case which-key-popup-type (minibuffer (which-key/show-buffer-minibuffer act-popup-dim)) (side-window (which-key/show-buffer-side-window act-popup-dim)) - (frame (which-key/show-buffer-frame act-popup-dim))))) + (frame (which-key/show-buffer-frame act-popup-dim)) + (custom (funcall #'which-key/custom-show-popup-function act-popup-dim))))) (defun which-key/show-buffer-minibuffer (act-popup-dim) "Does nothing. Stub for consistency with other show-buffer @@ -499,7 +517,8 @@ window." (cl-case which-key-popup-type (minibuffer (which-key/minibuffer-max-dimensions)) (side-window (which-key/side-window-max-dimensions)) - (frame (which-key/frame-max-dimensions)))) + (frame (which-key/frame-max-dimensions)) + (custom (funcall #'which-key/custom-popup-max-dimensions-function selected-window-width)))) (defun which-key/minibuffer-max-dimensions () "Return max-dimensions of minibuffer (height . width) in lines -- 2.30.2